package com.ruoyi.car.business.service.impl;


import com.ruoyi.car.business.domain.BusCustomer;
import com.ruoyi.car.business.mapper.BusCustomerMapper;
import com.ruoyi.car.business.query.BusCustomerQuery;
import com.ruoyi.car.business.service.IBusCustomerService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.GlobalException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class BusCustomerServiceImpl implements IBusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;

    @Override
    public TablePageInfo<BusCustomer> query(BusCustomerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BusCustomer>(busCustomerMapper.selectForList(qo));
    }


    @Override
    public void save(BusCustomer busCustomer) {
        if (busCustomer != null) {
            // 查询出所有手机号
            List<String> phoneList = busCustomerMapper.selectListPhone();
            for (String string : phoneList) {
                if (string.equals(busCustomer.getPhone())) {
                    throw new GlobalException("手机号不能相同");
                }
            }
        }
        busCustomerMapper.insert(busCustomer);
    }

    @Override
    public BusCustomer get(Long id) {
        return busCustomerMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BusCustomer busCustomer) {
        BusCustomer newBusCustomer = this.get(busCustomer.getId());
        newBusCustomer.setName(busCustomer.getName());
        newBusCustomer.setBirthday(busCustomer.getBirthday());
        newBusCustomer.setVip(busCustomer.getVip());
        newBusCustomer.setRemark(busCustomer.getRemark());
        busCustomerMapper.updateByPrimaryKey(newBusCustomer);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            busCustomerMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<BusCustomer> list() {
        return busCustomerMapper.selectAll();
    }

    @Override
    public Workbook export() {
        // 查询所有的客户信息, 封装workbook
        List<BusCustomer> customerList = this.list();
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("客户信息");
        // 定义标题头
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("编号");
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("联系方式");
        titleRow.createCell(3).setCellValue("生日");
        titleRow.createCell(4).setCellValue("是否VIP");
        titleRow.createCell(5).setCellValue("备注信息");
        for (int i = 0; i < customerList.size(); i++) {
            BusCustomer customer = customerList.get(i);
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(customer.getId());
            row.createCell(1).setCellValue(customer.getName());
            row.createCell(2).setCellValue(customer.getPhone());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(customer.getBirthday());
            row.createCell(3).setCellValue(birthday);
            String vip = customer.getVip() == 1 ? "是" : "否";
            row.createCell(4).setCellValue(vip);
            row.createCell(5).setCellValue(customer.getRemark());
        }
        return wb;
    }
}
